{-- Percussion Legato - version 1.1 - by Frank Condello http://chaoticbox.com/ --}

{ Parameters:

  Interval : Maximum time between hits (in seconds) before they are considered legato.

    Attack : The maximum fade-in time for legato hits (in seconds).

  Adaptive : When set to full (100) The attack rate is scaled down based on the velocity
			 of the legato hit, and how late it occurred. This is generally more realistic
			 but can introduce unwanted transients with some samples. Lowering the
			 Adaptive value scales this effect back, resulting in smoother but less
			 responsive legato. When Adaptive reaches zero, the maximum attack rate is
			 used for all legato hits.
	
Limiting the affected note range:

 Press the "Set_Note_Range" button and follow the prompts by hitting a key on the Kontakt
 graphical keyboard, or attached MIDI hardware to set the desired key range. The "Low" and
 "High" note values can also be manipulated directly.
}

on init
	
	{ Legato parameters }
	declare ui_knob $Interval (0, 1000, 1000)
	declare ui_knob $Attack (0, 1000, 1000)
	declare ui_knob $Adaptive (0, 100, 1)
	$Interval := 250
	$Attack := 150
	$Adaptive := 100
	make_persistent ($Interval)
	make_persistent ($Attack)
	make_persistent ($Adaptive)
	declare %startTime[127]
	declare polyphonic $currentTime
	declare polyphonic $fadeTime
	declare polyphonic $intervalDiff
	declare polyphonic $adaptInterval
	declare polyphonic $adaptVelocity
	declare polyphonic $attackUS
	
	{ Note Ranger }
	declare ui_value_edit $Low (0, 127, 1)
	declare ui_value_edit $High (0, 127, 1)
	declare ui_button $Set_Note_Range
	declare ui_label $RangeStateDisplay (1, 1)
	$High := 127
	set_text($RangeStateDisplay, "")
	make_persistent ($Low)
	make_persistent ($High)
	declare $setRangeState
	declare const $setRangeOff := 0
	declare const $setRangeLow := 1
	declare const $setRangeHigh := 2
	move_control($Low, 5, 2)
	move_control($High, 6, 2)
	move_control($Set_Note_Range, 5, 1)
	move_control($RangeStateDisplay, 6, 1)

	message (" ")
	
end on

on ui_control($Set_Note_Range)

	if ( $Set_Note_Range = 1 )
		$setRangeState := $setRangeLow
		set_text ($RangeStateDisplay, "Hit Low Note")
	else
		$setRangeState := $setRangeOff
		set_text ($RangeStateDisplay, "")
	end if

end on

on note

	{-- Range setting mode... --}
	
	if ( $setRangeState # $setRangeOff and $Set_Note_Range = 1 )
		if ( $setRangeState = $setRangeLow )
			$Low := $EVENT_NOTE
			set_text ($RangeStateDisplay, "Hit High Note")
			$setRangeState := $setRangeHigh
		else
			$High := $EVENT_NOTE
			$Set_Note_Range := 0
			$setRangeState := $setRangeOff
			set_text ($RangeStateDisplay, "")
		end if
		exit
	end if
	
	if ( $EVENT_NOTE < $Low or $EVENT_NOTE > $High )
		exit
	end if
	
	{-- Playback mode... --}
	
	$currentTime := $ENGINE_UPTIME
	$intervalDiff := $currentTime-%startTime[$EVENT_NOTE]
	
	if ( $intervalDiff < $Interval )
		
		if ( $Adaptive > 0 )
			
			$attackUS := $Attack*1000 { microseconds }
			
			{ Re-introduce some of the initial transient for late and/or hard hits }
			$adaptInterval := $attackUS / ( 1000 / ( ($intervalDiff*1000)/$Interval ) )
			$adaptVelocity := $attackUS / ( 1000 / ( ($EVENT_VELOCITY*1000)/127 ) )
			$fadetime := ($adaptInterval+$adaptVelocity)/2
			
			{ Scale back if not fully Adaptive }
			if ($Adaptive < 100)
				$fadetime := $fadetime - ($fadetime/$Adaptive)
			end if
			
			$fadetime := $attackUS - $fadetime

		else
			$fadetime := $Attack*1000 { Absolute }
		end if
		
		fade_in($EVENT_ID, $fadetime)
	
	end if
	
	%startTime[$EVENT_NOTE] := $currentTime
	
end on
